package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.VenueListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.CityAndTypeBean;
import com.bocai.yuweiwangluo.bean.VenueListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IVenueListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class VenueListPresenter
{
    private CityAndTypeBean cityAndTypeBean=new CityAndTypeBean();
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IVenueListView venueListView;
    private Context context;
    private BaseView baseView;
    private int requsttype;
    private VenueListBean venueListBean=new VenueListBean();
    VenueListAdapter mVenueListAdapter;
    public VenueListPresenter(Context context, IVenueListView venueListView, BaseView baseView)
    {
        this.context = context;
        this.venueListView = venueListView;
        this.baseView=baseView;
    }

    /**
     * 获取场地头部接口
     */
    public void getVenueListHeader()
    {
        requsttype=1;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getVenueListHeader(UrlData.BASEURL + UrlData.GETNAV, handler);
    }
    /**
     * 获取场地接口
     */
    public void getVenueList(String page,String type_id,String city_id)
    {
        baseView.showLoading();
        requsttype=2;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getVenueList(UrlData.BASEURL + UrlData.VENUELIST, page, type_id, city_id,venueListView.getVenue(),MyConst.location_lng+"",MyConst.location_lat+"" ,venueListView.getSortType(),handler);
    }
    public CityAndTypeBean getCityAndTypeBean()
    {

        return cityAndTypeBean;
    }
    public VenueListBean getVenueListBean()
    {

        return venueListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
//            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();

            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        if(requsttype==1){
                            venueListView.load();
                            Log.e("loginresult", result);
                            cityAndTypeBean = gson.fromJson(result, CityAndTypeBean.class);
                            MyConst.type=cityAndTypeBean.getType();
                            baseView.hideLoading();

                            if(venueListBean.getData().size()==0&venueListView.getPage()==1){
                                venueListView.getnoContent().setVisibility(View.VISIBLE);
                                venueListView.getListView().setVisibility(View.GONE);
                            }else
                            {
                                venueListView.getnoContent().setVisibility(View.GONE);
                                venueListView.getListView().setVisibility(View.VISIBLE);
                                if (venueListView.getPage()==1){
                                    mVenueListAdapter=new VenueListAdapter(context,venueListBean);
                                    venueListView.getListView().setAdapter(mVenueListAdapter);
                                }else{
                                    mVenueListAdapter.addData(venueListBean);

                                }
                                if(venueListBean.getTotal()<=(venueListBean.getEach()*venueListView.getPage())){
                                    venueListView.getListView().setPullLoadEnable(false);
                                }

                            }

                        }else{
                            Log.e("venue", result);
                            venueListBean= gson.fromJson(result, VenueListBean.class);
                            getVenueListHeader();
                        }
                        break;
                    case RESPONSE_FAILED:
                        venueListView.load();
                        baseView.hideLoading();
                        baseView.showError(result);
                        venueListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };

//    private void dataSortLocal() {
//        Comparator comparator=new Comparator()
//        {
//            @Override
//            public int compare(Object o1, Object o2)
//            {
//                VenueListBean.Data data1=(VenueListBean.Data)o1;
//                VenueListBean.Data data2=(VenueListBean.Data)o2;
//                if(Double.parseDouble(data1.getMin_local())<Double.parseDouble(data2.getMin_local()))
//                {
//                    return -1;
//                }else if(Double.parseDouble(data1.getMin_local())==Double.parseDouble(data2.getMin_local()))
//                {
//                    return 0;
//
//                }else if(Double.parseDouble(data1.getMin_local())>Double.parseDouble(data2.getMin_local()))
//                {
//                    return 1;
//                }
//                return 0;
//            }
//        };
//        Collections.sort(venueListBean.getData(),comparator);
//    }
//    private void dataSortPrice() {
//        Comparator comparator=new Comparator()
//        {
//            @Override
//            public int compare(Object o1, Object o2)
//            {
//                VenueListBean.Data data1=(VenueListBean.Data)o1;
//                VenueListBean.Data data2=(VenueListBean.Data)o2;
//                if(Double.parseDouble(data1.getPrice())<Double.parseDouble(data2.getPrice()))
//                {
//                    return -1;
//                }else if(Double.parseDouble(data1.getPrice())==Double.parseDouble(data2.getPrice()))
//                {
//                    return 0;
//
//                }else if(Double.parseDouble(data1.getPrice())>Double.parseDouble(data2.getPrice()))
//                {
//                    return 1;
//
//                }
//                return 0;
//            }
//        };
//        Collections.sort(venueListBean.getData(),comparator);
//    }
}